<template>
  <div class="s-box">
    <view-box ref="viewBox" body-padding-top="0" body-padding-bottom="0">

    </view-box>
  </div>
</template>

<script>
  import {ViewBox} from 'vux'

  export default {
    components: {
      ViewBox
    },
    data() {
      return {}
    },
    created() {
//      this.$router.replace('/');
      this.wechatLogin();
    },
    methods: {
      wechatLogin() {

        if (typeof this.getParam('code') === 'undefined') {
          this.$vux.toast.show({
            text: '链接错误，请在微信中打开',
            position: 'middle',
            width: 'auto'
          });
          this.$router.replace('/404');
        } else {
          this.$api.wechatLogin({
            code: this.getParam('code')
          }).then(data => {
            if (data.returnCode === 1) {
              localStorage.setItem('user_token', data.user.token);
              localStorage.setItem('user_userId', data.user.userId);
              localStorage.setItem('user_openId', data.user.wechatOpenId);
              this.$router.replace('/');

            } else if (data.returnCode === -1 || data.returnCode === -2) {
              localStorage.setItem('user_openId', data.user.wechatOpenId);
              this.$router.replace('/login');
            } else {
              this.$vux.toast.show({
                text: '链接错误，请在微信中打开',
                position: 'middle',
                width: 'auto'
              });
              this.$router.replace('/404');

            }
          })
        }
      },

      //获取链接中某个参数 origin-设置来源
      getParam(param, origin) {
        let reg = new RegExp(param + "=(d*[a-zA-Z]*[^?|^#|^&]*)"),
          str = (origin || location.href).match(reg);
        if (str) {
          str = decodeURIComponent(str[1]);
          return str;
        }
      },
    }
  }
</script>

<style lang="less" scoped>
  .logo {
    margin-top: 10px;
    text-align: center;
    font-size: 1.2rem;
    color: #666666;
    img {
      width: 4rem;
    }
    p {
      font-size: .8rem;
    }
  }

  .sendButton {
    width: 3rem;
    font-size: .33rem;
  }
</style>
